<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="../../assets/css/maincss.css" rel="stylesheet" />
<link href="../../assets/css/but.css" rel="stylesheet" />
</head>
<!-- Bootstrap -->
<link href="${pageContext.request.contextPath}/javascript/bootstrap/css/bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="${pageContext.request.contextPath}/javascript/table/bootstrap-table.css">
<script src="${pageContext.request.contextPath}/javascript/jQuery-1.12.4.js"></script>
<script src="${pageContext.request.contextPath}/javascript/bootstrap/js/bootstrap.js"></script>
<script src="${pageContext.request.contextPath}/javascript/table/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/javascript/table/locale/bootstrap-table-zh-CN.js"></script>
<body>
	<h3>类别管理</h3>
	<table  style="text-align: center;" >
		<tr>
			<td colspan="3" style="text-align: left;">
				<input type="text" id="currentPage" name="currentPage" value="1" hidden />
				<input type="text" id="pageSize" name="pageSize" value="5" hidden />
				<div class="form-inline">
					<input class="form-control" type="text" id="seachCateName" name="seachCateName" placeholder="请输入供应商名称" />
					<button class="btn btn-info" onclick="searchCategoryBut()" class="but green">查询</button>
				</div>
			</td>
			<td style="align: right;">
				<button class="btn btn-success" onclick="addCategoryBut()" class="but green">增 加</button>
			</td>
		</tr>
	</table>
	<table id="myTable"  style="text-align: center;" >
		<!-- <tr>
			<td>类别id</td>
			<td>类别名称</td>
			<td>类别简介</td>
			<td>操作</td>
		</tr> -->
		
	</table>
	

</body>

<script type="text/javascript">

//------------------------bootstrap-table----------------------------------
$(function () {

    //1.初始化Table
    var oTable = new TableInit();
    oTable.Init();
});

var TableInit = function () {
    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
        $('#myTable').bootstrapTable({
            url: 'CategoryServlet',         //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            pagination: true,                   //是否显示分页（*）
            
            queryParams: oTableInit.queryParams,//传递参数（*）
            
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            
            pageNumber:1,                       //初始化加载第一页，默认第一页
            pageSize: 5,                       //每页的记录行数（*）
            pageList: [5, 10, 25, 50, 100],        //可供选择的每页的行数（*）
            // height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            columns: [{
                checkbox: true
            }, {
                field: 'categoryId',
                title: '类别id'
            }, {
                field: 'categoryName',
                title: '类别名称'
            }, {
                field: 'categoryDesc',
                title: '类别简介'
            }, {
                field: 'Desc',
                title: '操作',
                formatter : actionFormatter
            } ]
        });
    };

    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit: params.limit,   //页面大小
            offset: params.offset,  //页码[忽略数]
            op : "doSearchCategory",
            cateName : $("input[name='seachCateName']").val()
        };
        return temp;
    };
    return oTableInit;
};


	function actionFormatter(value , row , index){
		// alert("value : "+value+"===row : "+row+"===index : "+index);
		return "<button onclick='editCategoryById("+row.categoryId+")' class='btn btn-success btn-sm'>修 改</button>&nbsp;"+
			"<button onclick='return delCategoryById("+row.categoryId+")' class='btn btn-danger btn-sm'>删 除</button>";
	}


//------------------------bootstrap-table----------------------------------


	// 点击条件查询
	function searchCategoryBut(){
	
		$("#myTable").bootstrapTable('refresh',{
			query : {
				'limit' : '5',
				'offset' : '0',
				'cateName' : $("input[name='seachCateName']").val()
			}
		});
		$("#myTable").bootstrapTable('refreshOptions',{
			pageNumber : 1
		});
		
	
	}
	
	searchCategoryBut();
	
	// 修改
	function editCategoryById(id){
		location.href = 'CategoryServlet?op=readyEditCategoryById&id='+id;
	}
	
	// 删除
	function delCategoryById(id){
		var msg = "确定删除该条数据?";
		if(confirm(msg) == true){
			location.href='CategoryServlet?op=removeCategoryById&id='+id;
			return true;
		} else {
			return false;	
		}
	}
	
	// 增加
	function addCategoryBut(){
		location.href='addCategory.jsp';
	}



</script>

</html>
